/* $RCSFile: ModuleDefinitions.ascx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/ModuleDefinitions/ModuleDefinitions.ascx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:21  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:43:56  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:25  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Globalization;

using AddonNice.Core;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Security;
using AddonNice.Configuration;
using AddonNice.Configuration.Settings;
using AddonNice.Diagnostics;
using AddonNice.UI.DataTypes;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.PageModules.ModuleDefinitions
{
    public class AStr
    {
            public const string RepeatDirectionSetting      =   "MODULEDEFINITION_REPEATDIRECTION";
            public const string RepeatColumns               =   "MODULEDEFINITION_REPEATCOLUMNS";
            public const string ShowPath                    =   "MODULEDEFINITION_SHOWPATH";
            public const string ModuleCategory              =   "MODULEDEFINITION_MODULECATEGORY";
    }

    /// <summary>
    /// Initializer class for ModuleDefinitions 
    /// </summary>
    public class ModuleDefinitionsSI : PortalModuleControlSI
    {
		ModulesDB mDB                                       =   new ModulesDB();
        public ModuleDefinitionsSI(ModuleSettingsDictionary bSettings,ISettingsOwner pm) : base()
        {
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]ModuleDefinitionsSI Ctor BEG.",ModuleTraceSwitch.Sw.Info);
            InitSettings(bSettings,pm);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]ModuleDefinitionsSI Ctor END.",ModuleTraceSwitch.Sw.Info);
        }
 
        public override void InitSettings(ModuleSettingsDictionary bSettings,ISettingsOwner pm)
        {             
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]ModuleDefinitionsSI InitSettings BEG.",ModuleTraceSwitch.Sw.Info);
            
            base.InitSettings(bSettings,pm);

            string extgrp                       =   pm.ExtendedResourcesGroup;
            
			SettingItemSpecial RepeatDirection  =	new SettingItemSpecial(AStr.RepeatDirectionSetting,new ListDataType("Vertical;Horizontal"));
			RepeatDirection.Required	        =	true;
			RepeatDirection.Value		        =	"Vertical";
			RepeatDirection.Order		        =	1;	
			RepeatDirection.ExtendedGroupName   =    extgrp;
            RepeatDirection.Description		    =	"Repeat Direction";
			RepeatDirection.EnglishName		    =	"Repeat Direction";
			bSettings.AddSetting(RepeatDirection);

			SettingItemSpecial RepeatColumn	    =	new SettingItemSpecial(AStr.RepeatColumns,new IntegerDataType());
			RepeatColumn.Required		        =	true;
			RepeatColumn.Value			        =	"1";
			RepeatColumn.MinValue		        =	1;
			RepeatColumn.MaxValue		        =	10;
			RepeatColumn.Order			        =	20;
			RepeatColumn.ExtendedGroupName      =    extgrp;
            RepeatColumn.Description		    =	"Columns number";
			RepeatColumn.EnglishName		    =	"Columns number";
			bSettings.AddSetting(RepeatColumn);

			SettingItemSpecial ShowPath	        =	new SettingItemSpecial(AStr.ShowPath,new BooleanDataType());
			ShowPath.Required		            =	true;
			ShowPath.Value			            =	StStr.strFalse;
			ShowPath.Order			            =	25;
			ShowPath.ExtendedGroupName          =    extgrp;
            ShowPath.Description		        =	"Show Module Path";
			ShowPath.EnglishName		        =	"Show Path";
			bSettings.AddSetting(ShowPath);

            // Get all existing module categories from DB
            DataTable tbCat                     =   mDB.GetCurrentModuleCategoriesTb(Owner.PortalID);
            DataRow rw                          =   tbCat.NewRow();
            // Add All
            rw[StStr.DBCategory]                =   "All";
            tbCat.Rows.InsertAt(rw,0);
			SettingItemSpecial ModuleCategory   =	new SettingItemSpecial(AStr.ModuleCategory,new CustomListDataType(tbCat,StStr.DBCategory,StStr.DBCategory));
			ModuleCategory.Required		        =	true;
			ModuleCategory.Value			    =	"All";
			ModuleCategory.Order			    =	30;
			ModuleCategory.ExtendedGroupName    =    extgrp;
            ModuleCategory.Description		    =	"Modules category";
			ModuleCategory.EnglishName		    =	"Select the category for modules in the list.";
			bSettings.AddSetting(ModuleCategory);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]ModuleDefinitionsSI InitSettings END.",ModuleTraceSwitch.Sw.Info);
        }
    }

    /// <summary>
    /// Module that shows available modules for current portal (readonly)
    /// </summary>
    public partial class ModuleDefinitions : PortalModuleControl
    {
		ModulesDB mDB                                       =   new ModulesDB();

        /// <summary>
		/// Admin Module
		/// </summary>
		public override bool AdminModule
		{
			get
			{
				return true;
			}
		}

		public override Guid GuidID 
		{
			get
			{
				return new Guid(StStr.ModulesDefinitionsGuid);
			}
		}

        public VersionType GetVersion(int vType)
        {
            return (VersionType)vType;
        }
        
        public bool ShowModulePath
        {
            get
            {
                return Settings[AStr.ShowPath].ToBool();
            }
        }

        /// <summary>
        /// Ctor params repeat di, columns, cat
        /// </summary>
        public ModuleDefinitions()
        {
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]ModuleDefinitions Ctor Called.",ModuleTraceSwitch.Sw.Info);
        }

        public override string ExtendedResourcesGroup 
		{
            get
            {
       			const string extgrp                 =   "MODULEDEFINITIONS";
                return extgrp;
            }
		}

        public override void InitPortalModuleControl(ModuleSettingsDictionary bSettings)
        {
            PortalModuleInitializer =   new ModuleDefinitionsSI(bSettings,this);
        }

        private void Page_Load(object sender, System.EventArgs e) 
        {
            // If this is the first visit to the page, bind the definition data to the datalist
            if (! IsPostBack ) 
                BindData();
        }

        /// <summary>
        /// The BindData helper method is used to bind the list of 
        /// module definitions for this portal to an asp:datalist server control
        /// </summary>
        void BindData()
        {
			defsList.RepeatDirection			=	(Settings[AStr.RepeatDirectionSetting].ToString() == "Horizontal" ? 
                                                        RepeatDirection.Horizontal : RepeatDirection.Vertical);
			defsList.RepeatColumns			    =	Settings[AStr.RepeatColumns].ToInt();
            string catgory                      =   Settings[AStr.ModuleCategory].Value;
            // Get the portal's defs from the database
            SqlDataReader dr        =   null;
			try
			{
                if ( catgory == "All" )
				        dr	                =	new ModulesDB().GetCurrentModuleDefinitions(portalSettings.PortalID);
                else
                {
                        dr	                =	new ModulesDB().GetCurrentModuleDefinitionsByCategory(portalSettings.PortalID,catgory);
                }
				defsList.DataSource = dr;
				defsList.DataBind();
			}
			catch(Exception ex)
			{
                if ( ModuleTraceSwitch.Sw.TraceError )
                    System.Diagnostics.Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ModuleDefinitions ex : {0}", ex),ModuleTraceSwitch.Sw.Info);
                throw;
			}
			finally
			{
				if ( dr != null )
					dr.Close();
			}

        }



		#region Std Overrides

        override protected void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}

        private void InitializeComponent() 
        {
			Load        +=  new System.EventHandler(Page_Load);
		}
		#endregion
    }
}
